// /** 卡牌颜色 */

import { _decorator, Component, Node, UITransform, v3, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

import SSS_TimerManager from "../../../../Script/Manager/SSS_TimerManager";
import DragTetris_GameConfig from "../Data/DragTetris_GameConfig";
import { DragTetris_GameData } from "../Data/DragTetris_GameData";
import DragTetris_Block from "./DragTetris_Block";
enum cardColor {
    CARD_0 = "#969696",
    CARD_1 = "#C8C8C8",
}

@ccclass('DragTetrisGrid')
export default class DragTetris_Grid extends Component {
    @property(Node)
    red: Node | null = null;
    //    /** 选中框节点 */
    @property(Node)
    select: Node | null = null;
    @property(Node)
    X: Node | null = null;
    //    // @property(Node)
    //    // frame: Node = null;
    //    // @property(Node)
    //    // frame_up: Node = null;
    //    // @property(Node)
    //    // frame_down: Node = null;
    //    // @property(Node)
    //    // frame_left: Node = null;
    //    // @property(Node)
    //    // frame_right: Node = null;
    //    // @property(Label)
    //    // Label_position: Label = null;
    id: number = 0;
    x: number = 0;
    y: number = 0;
    block: DragTetris_Block = null;
    canPlace: boolean = true;
    //    // 只序列化需要的属性
    toJSON() {
        let block = this.block;
        if (block) {
            return {
                // blockType: block.blockType,
                x: this.x,
                y: this.y,
                color: block.color,
                point: block.point,
                // // _class: 'MyClass' // 添加类标识以便反序列化
            };
        }

    }
    //    // 初始化
    init(_id: number, _x: number, _y: number) {
        this.id = _id;
        this.x = _x;
        this.y = _y;
        this.block = null;

        // const colorIndes = (((this.y + 1) % 2) + ((this.x + 1) % 2)) % 2;
        // this.node.color = new Color().fromHEX(cardColor["CARD_" + colorIndes]);

        this.initWidthAndHeight(DragTetris_GameConfig.size, DragTetris_GameConfig.size);
        this.initPosition();

        this.Refresh();
        // this.AddListener();
    }
    RefreshPlace() {
        this.X.active = !this.canPlace;
        // this.X.active = true;
    }
    GetNodeWorldPostation(_node: Node): Vec3 {
        let pos: Vec3 = v3();
        if (_node.parent == null) {
            pos = v3(_node.getPosition());
        } else {
            // pos = v3(_node.parent.convertToWorldSpaceAR(_node.getPosition()));
            pos = _node.getWorldPosition();
        }
        return pos;
    }
    TOUCH_END() {
        DragTetris_GameData.IsMouseDown = false;
    }
    AddListener() {
        this.node.on(
            Node.EventType.TOUCH_END,
            (t) => {
                this.TOUCH_END();
            },
            this
        );
        this.node.on(
            Node.EventType.TOUCH_CANCEL,
            (t) => {
                this.TOUCH_END();
            },
            this
        );

        this.node.on(Node.EventType.TOUCH_MOVE, (t) => { }, this);

        this.node.on(Node.EventType.TOUCH_START, (t) => { }, this);
    }
    Main() { }
    Refresh() {
        this.node.active = this.id >= 0;
        // this.Label_position.string = `${this.x}-${this.y}`;
    }
    Select(select: boolean): void {
        this.select.active = select;
    }
    //    // 初始化单元宽高
    initWidthAndHeight(cardWidth?: number, cardHeight?: number) {
        if (cardWidth) this.node.getComponent(UITransform).width = cardWidth;
        if (cardHeight) this.node.getComponent(UITransform).height = cardHeight;
    }
    //    // 初始化位置
    initPosition() {
        let x = -DragTetris_GameConfig.W / 2 + this.node.getComponent(UITransform).width / 2 + this.node.getComponent(UITransform).width * this.x;
        let y = -DragTetris_GameConfig.H / 2 + this.node.getComponent(UITransform).height / 2 + this.node.getComponent(UITransform).height * this.y;
        this.node.position = v3(x, y, 0);
    }
    async Flash() {
        let duration = 0.2;
        this.red.active = true;
        await SSS_TimerManager.AwaitTime(duration);
        this.red.active = false;

        await SSS_TimerManager.AwaitTime(duration);
        this.red.active = true;
        await SSS_TimerManager.AwaitTime(duration);
        this.red.active = false;

        await SSS_TimerManager.AwaitTime(duration);
        this.red.active = true;
        await SSS_TimerManager.AwaitTime(duration);
        this.red.active = false;

        // Tween.stopAllByTarget(this.select);
        // tween(this.select)
        //     .repeatForever(
        //         tween()
        //             // .to(0.4, { opacity: 0 })
        //             // .to(0.4, { opacity: 255 })
        //             // .delay(0.4)
        //             // .call(() => {
        //             //     this.select.opacity = 255;
        //             // })
        //             // .delay(0.4)
        //             // .call(() => {
        //             //     this.select.opacity = 0;
        //             // })


        //             .to(0.4, { scale: 0.98 })
        //             .to(0.4, { scale: 0.90 })
        //     )
        //     .start()
    }
}